//
//  LoginNet.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/9.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "LoginNet.h"
#import "BasicNetManager.h"
#import "BLHeader.h"
#import "User.h"
#import "NSString+SortNameUtils.h"
#import "JPUSHService.h"
#import <YYModel.h>

@implementation LoginNet

#pragma mark 登录  密码登录/ 验证码登录
+ (void)postWithTel:(NSString *) tel password:(NSString *)password loginType:(BOOL) isCode completer:(void(^)(id resultObjec,NSError *error))completer{
    NSDictionary *dic;
    NSString *url;
    if(isCode) {
        url = MSGLogin;
        dic = @{@"code" : password, @"userTel" : tel};
    }else {
        url = LoginURL;
        // 加密
        NSString *pwdMd5 = [password md5String];
        dic = @{@"userTel":tel, @"passWord":pwdMd5};

    }
    BasicNetManager *manager = [BasicNetManager share];
    [manager postURL:url param:dic completer:^(id result, NSError *error) {
        if(completer) {
            User *user;
            if(result) {
                user = [User yy_modelWithJSON:result];
            }
            NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
            [userDefault setObject:user.userName  forKey:USERNAME];
            [userDefault setObject:user.passWord ? user.passWord : @" " forKey:USERPWD];
            [userDefault setObject:user.signature ? user.signature : @" " forKey:USERSIGN];
            [userDefault setObject:user.userAddr ? user.userAddr : @" " forKey:USERADDR];
            [userDefault setObject:user.userClass forKey:USERCLASS];
            [userDefault setObject:user.userNum ? user.userNum : @" " forKey:USERNUM];
            [userDefault setObject:user.userPay ? user.userPay : @" " forKey:USERPAY];
            [userDefault setObject:user.userSex ? user.userSex : @" " forKey:USERSEX];
            [userDefault setObject:user.userTel forKey:USERTEL];
            [userDefault setObject:user.userImg ? user.userImg : @" " forKey:USERIMG];
            [userDefault synchronize];
            [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
                [JPUSHService setAlias:user.userTel completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                } seq: 0];
            }];
            completer(user, error);
        }
    }];
}


#pragma mark 微信登录
+ (void)weChatLoginGetToken:(NSString *)code completer:(void(^)(id result, NSError *error))completer {
    BasicNetManager *manager = [BasicNetManager share];
    NSDictionary *param = @{@"code" : code};
    //NSDictionary *param = nil;
    [manager postURL:WECHATLOGIN param:param completer:^(id result, NSError *error) {
        if(result) {
            NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
            [userDefault setObject:[result objectForKey:@"nickname"]  forKey:USERNAME];
            [userDefault setObject:[result objectForKey:@"sex"] forKey:USERSEX];
            [userDefault setObject:[result objectForKey:@"headImgUrl"] forKey:USERIMG];
            [userDefault setObject:[result objectForKey:@"userTel"] forKey:USERTEL];
            [userDefault setObject:[result objectForKey:@"userClass"] forKey:USERCLASS];
            [userDefault setObject:[result objectForKey:@"openid"] forKey:WECHATE_OPENID];
            [userDefault setObject:@"1" forKey:LOGINWAY];
            [userDefault synchronize];
            [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
                [JPUSHService setAlias:[result objectForKey:@"userTel"] completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                } seq: 0];
            }];
        }
        if (completer) {
            completer(result, error);
        }
    }];
}


#pragma mark QQ登录
+ (void)QQLoginToken:(NSString *)token openID:(NSString *)openid completer:(void(^)(id resultObject, NSError *error))completer {
    NSDictionary *param = @{@"accessToken":token, @"openId":openid};
    //NSLog(@"param = %@",param);
    //NSDictionary *param = nil;
    BasicNetManager *manager = [BasicNetManager share];
    [manager postURL:QQLOGIN param:param completer:^(id result, NSError *error) {
        if (completer) {
            if (result) {
                NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
                [userDefault setObject:[result objectForKey:@"nickname"]  forKey:USERNAME];
                [userDefault setObject:[result objectForKey:@"gender"] forKey:USERSEX];
                [userDefault setObject:[result objectForKey:@"figureurl_2"] forKey:USERIMG];
                [userDefault setObject:[result objectForKey:@"userTel"] forKey:USERTEL];
                [userDefault setObject:[result objectForKey:@"userClass"] forKey:USERCLASS];
                [userDefault setObject:[result objectForKey:@"openId"] forKey:WECHATE_OPENID];
                [userDefault setObject:@"2" forKey:LOGINWAY];
                [userDefault synchronize];
                [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
                    [JPUSHService setAlias:[result objectForKey:@"userTel"] completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                    } seq: 0];
                }];
            }
            completer(result, error);
        }
    }];
}


#pragma mark 微博登录
+ (void)WEIBOLoginToken:(NSString *)token openID:(NSString *)uid completer:(void(^)(id resultObject, NSError *error))completer {
    NSDictionary *param = @{@"accessToken":token, @"uid":uid};
    //NSLog(@"param = %@",param);
    //NSDictionary *param = nil;
    BasicNetManager *manager = [BasicNetManager share];
    [manager postURL:WEIBOLOGIN param:param completer:^(id result, NSError *error) {
        if (completer) {
            if (result) {
                NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
                [userDefault setObject:[result objectForKey:@"name"]  forKey:USERNAME];
                [userDefault setObject:[result objectForKey:@"gender"] forKey:USERSEX];
                [userDefault setObject:[result objectForKey:@"profile_image_url"] forKey:USERIMG];
                [userDefault setObject:[result objectForKey:@"userTel"] forKey:USERTEL];
                [userDefault setObject:[result objectForKey:@"userClass"] forKey:USERCLASS];
                [userDefault setObject:[result objectForKey:@"uid"] forKey:WECHATE_OPENID];
                [userDefault setObject:@"3" forKey:LOGINWAY];
                [userDefault synchronize];
                [JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
                    [JPUSHService setAlias:[result objectForKey:@"userTel"] completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                    } seq: 0];
                }];
            }
            completer(result, error);
        }
    }];
}

#pragma mark 注册
+ (void)postRegister:(NSDictionary *)param completer:(void (^)(id resultObject, NSError *error))completer {
    BasicNetManager *manager = [BasicNetManager share];
    [manager postURL:RegisterURL param:param completer:^(id result, NSError *error) {
        if(completer) {
            completer(result, error);
        }
    }];
}

#pragma mark 获取验证码 注册验证码：1   登录验证码 2  重置验证码 3
+ (void)getCodeWithTel:(NSString *)tel codeType:(NSInteger) codeType completer:(void(^)(id resultObject, NSError *error))completer {
    NSDictionary *param = @{@"userTel": tel};
    NSString *url;
    if(codeType == 1) {
        url = CodeURL;
    }else if (codeType == 2) {
        url = LoginCodeURL;
    }else if (codeType == 3) {
        url = ResetPWDCodeURl;
    }
    BasicNetManager *manager = [BasicNetManager share];
    [manager postURL:url param:param completer:^(id result, NSError *error) {
        if(completer) {
            completer(result, error);
        }
    }];
}

#pragma mark 重置密码
+ (void)resetPWd:(NSDictionary *)param completer:(void (^)(id resultObject, NSError *error))completer {
    BasicNetManager *manager = [BasicNetManager share];
    [manager postURL:ResetPwdURL param:param completer:^(id result, NSError *error) {
        if(completer) {
            completer(result, error);
        }
    }];
}




@end
